package com.graduation.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation.springbootinit.common.ErrorCode;
import com.graduation.springbootinit.exception.BusinessException;
import com.graduation.springbootinit.mapper.ProductCommentMapper;
import com.graduation.springbootinit.model.entity.ShopPart.ProductComment;
import com.graduation.springbootinit.service.ProductCommentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductCommentServiceImpl extends ServiceImpl<ProductCommentMapper, ProductComment>
        implements ProductCommentService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addComment(ProductComment comment) {
        if (comment.getRating() != null && (comment.getRating() < 1 || comment.getRating() > 5)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "评分必须在1-5之间");
        }
        save(comment);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteComment(Long id, Long userId) {
        // 验证是否是评论作者
        ProductComment comment = getById(id);
        if (comment == null || !comment.getUserId().equals(userId)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "无权删除该评论");
        }
        // 删除评论及其回复
        remove(new QueryWrapper<ProductComment>()
                .eq("id", id)
                .or()
                .eq("parent_id", id));
    }

    @Override
    public void updateComment(ProductComment comment) {
        // 验证评论是否存在
        ProductComment existingComment = getById(comment.getId());
        if (existingComment == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "评论不存在");
        }
        
        // 验证是否是评论作者
        if (!existingComment.getUserId().equals(comment.getUserId())) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "无权修改该评论");
        }
        
        // 验证评分范围
        if (comment.getRating() != null && (comment.getRating() < 1 || comment.getRating() > 5)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "评分必须在1-5之间");
        }
        
        // 更新评论
        updateById(comment);
    }

    @Override
    public List<ProductComment> getCommentsByProductId(Long productId) {
        QueryWrapper<ProductComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId)
                .orderByDesc("created_at");
        return list(queryWrapper);
    }

    @Override
    public Page<ProductComment> getProductComments(Long productId, int pageNum, int pageSize) {
        Page<ProductComment> page = new Page<>(pageNum, pageSize);
        return baseMapper.selectPageByProductId(page, productId);
    }

    @Override
    public List<ProductComment> getCommentReplies(Long commentId) {
        return baseMapper.selectRepliesByParentId(commentId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void likeComment(Long commentId) {
        baseMapper.incrementLikeCount(commentId);
    }

    @Override
    public List<ProductComment> getUserComments(Long userId) {
        QueryWrapper<ProductComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .orderByDesc("created_at");
        return list(queryWrapper);
    }

    @Override
    public Long getCommentCount(Long productId) {
        return baseMapper.countByProductId(productId);
    }

    @Override
    public Double getAverageRating(Long productId) {
        return baseMapper.getAverageRating(productId);
    }
}